﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/15 11:40:00
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary.Forms;
using System;
using System.Windows.Forms;

namespace CommonLibrary
{
    /// <summary>
    /// 报警信息编辑窗体
    /// </summary>
    public partial class FormAlarmEdit : HWFrmWithTitle
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="alarm"></param>
        public FormAlarmEdit(AlarmEntity alarm = null)
        {
            InitializeComponent();
            LoadAlarm(alarm);
            this.Title = alarm != null ? "编辑报警信息" : "新增报警信息";
        }
        private void LoadAlarm(AlarmEntity alarm = null)
        {
            string code = string.Empty, name = string.Empty, address = string.Empty;
            if (alarm != null)
            {
                code = alarm.Code;
                name = alarm.Name;
                address = alarm.PLCAddress;
                this.labelID.Tag = alarm;
            }
            this.textBoxAlarmCode.Text = code;
            this.textBoxAlarmName.Text = name;
            this.textBoxPLCAddress.Text = address;
            if (!string.IsNullOrWhiteSpace(code))
            {
                //不可以编辑
                this.textBoxAlarmCode.ReadOnly = true;
                this.textBoxAlarmCode.Enabled = false;
            }
        }
        private void FormAlarmEdit_Load(object sender, EventArgs e)
        {
            this.labelStart.Select();
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            string code = this.textBoxAlarmCode.Text;
            string name = this.textBoxAlarmName.Text;
            string address = this.textBoxPLCAddress.Text;
            AlarmEntity alarm = this.labelID.Tag == null ? new AlarmEntity() : (AlarmEntity)this.labelID.Tag;
            bool isNew = this.labelID.Tag == null ? true : false;
            alarm.Code = code;
            alarm.Name = name;
            alarm.PLCAddress = address;
            if (AlarmHelper.ALARM_HELPER == null)
            {
                HWMessageBox.Show(this, "报警帮助类未初始化");
            }
            else
            {
                if (isNew)
                {
                    if (AlarmHelper.ALARM_HELPER.AddAlarm(alarm))
                    {
                        HWMessageBox.Show(this, "新增报警配置成功");
                        this.Close();
                    }
                    else
                    {
                        HWMessageBox.Show(this, "新增报警配置失败");
                    }
                }
                else
                {
                    if (AlarmHelper.ALARM_HELPER.EditAlarm(alarm))
                    {
                        HWMessageBox.Show(this, "保存报警配置成功");
                        this.Close();
                    }
                    else
                    {
                        HWMessageBox.Show(this, "保存报警配置失败");
                    }
                }
            }
        }
        private void buttonMaxSize_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
            }
        }
        private void FormAlarmEdit_Shown(object sender, EventArgs e)
        {
        }
        private void FormAlarmEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
        }
    }
}
